var barList = ["../index/index","../category/category","../member/member","../cart/cart"];
var index = '../index/index';
var members = '../login/login';
//API
function curlApi(url,method,header,data,that){
    if(!method || !url){
        return false;
    }
    if(!header){
        header = 'application/x-www-form-urlencoded;';
    }
     wx.request({
        url: url,
        method: method,
        data:data,
        header:{
            'content-type': header
        },
        // 获取API成功后操作
        success: function(res){
            console.log('成功',res);
        },
        // 获取API失败后操作
        fail: function(res){
            console.log(res)
        }
        
    });
    return true;
}
//弹出框
function message(content,t,s = 1000){
    if(!content){
        content = "没有内容";
    }
    if(t == 1){
       var ic = "success";
     }else{
       var ic = "loading";
     }

      wx.showToast({
        title: content,
        duration: s,
        icon: ic
      })
}
function getMid  (t = 1){
    var mid = 0;
    if(t == 1){
        //获取异步mid
        wx.getStorage({
            key: 'mid',
            success: function(res) {
                mid = res.data;
                console.log('获取异步用户ID：'+mid)
            } ,
            fail:function (res){
                console.log('获取异步用户ID失败')
            }
        })
    }else{
        //获取同步mid
        mid = wx.getStorageSync('mid');
    }
    return mid;

}
//获取用户车型
function getCarSeriesId()
{
    var car_series_id = 0;
    car_series_id = wx.getStorageSync('car_series_id');
    if(car_series_id == '')
    {
        car_series_id = 0;
    }
    return car_series_id;
}
//获取同步/异步缓存openid
function getOpenid (t = 1){
    var openid = '';
    if(t == 1){
        //获取异步openid
        wx.getStorage({
            key: 'openid',
            success: function(res) {
                openid = res.data;
            } 
        })
    }else{
        //获取同步openid
        openid = wx.getStorageSync('openid');
    }

    return openid;
}
//判断是否存在数组
function contains(arr, obj) {  
    var i = arr.length;  
    while (i--) {  
        if (arr[i] === obj) {  
            return true;  
        }  
    }  
    return false;  
} 
function checkLogin(mid,url,t = 1){
    //判断是否登录
    //var checkid = wx.getStorageSync('mid');
    var checkid    = wx.getStorageSync('suid');
    if(!checkid || checkid <= 0){  
      url = members;
    }
    //console.log(checkid,url,t);
    goLocation(url,t);
}
function getBarList(){
    var list = wx.getStorageSync('barlist'); 
    if(list){
        return list;
    }else{
        return barList;
    }
}
//组合跳转方式
function goLocation(url,t){
    
    //判断链接是否存在
    if(!url){
        url = index;
        t = 5;
    }

    //判断打开的页面是否到达5个
    var ob = getCurrentPages();
    if(ob.length == 5 && t != 3){
        t = 1;
    }
    
    var autoFooter =  wx.getStorageSync('autoFooter'); //获取是否开启自定义底部

    //判断是否导航页
    var has = false;
    if(autoFooter != 1){
        var bar = getBarList();
        has = contains(bar,url);
        
    }
    if(has){
        t = 2;
    }else{
        if(t == 2){
            if(url > 0){
                t = 3;
            }else{
                t = 1;
            }   
        }
    }
    
    
    if(t == 1){
        wx.redirectTo({
            url: url
        })
    }else if(t == 2){
        wx.switchTab({
            url: url
        })
    }else if(t == 3){
        wx.navigateBack({
            delta: url
        })
    }else{
        wx.navigateTo({
            url: url,
        })
    }

}
//前往导航栏页面
function goIndex(mid,url){
    if(!url){
        url = index;
    }
    wx.switchTab({
        url: url,
    })
}

function setMid(that,ses){
    var sesMid = getMid(2);
    var sesOpenid = getOpenid(2);
    if(sesMid > 0){
        that.setData({
            mid : sesMid,
            openid : sesOpenid
        })
    }
    return true;
}
module.exports.curlApi = curlApi
exports.message = message
exports.getMid = getMid
exports.getOpenid = getOpenid
exports.contains = contains
exports.checkLogin = checkLogin
exports.goLocation = goLocation
exports.goIndex = goIndex
exports.setMid = setMid
exports.getCarSeriesId =  getCarSeriesId